<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {

	
     function __construct() {
        parent::__construct();
        $this->load->model('users_db');
        $this->load->library('session');
        $this->load->helper('form');
        
     }
     
	public function index()
	{
		if($this->users_db->isLoggedIn())
     		{
         		 redirect('/admin/home/dashboard','refresh');
     		} 
     		else{
		
		$this->load->helper('form');
		$data['page_title'] = 'Admin Login';
		$data['main_content'] = 'login.php';
		$this->load->view('admin/login',$data);
     		}
	}
	
	public function dashboard()
	{
		     if($this->users_db->isLoggedIn() && $this->session->userdata('level') == 3 )

     { 
		$data['page_title'] = 'Dashboard';
		$data['folder'] = 'home';
		$data['main_content'] = 'dashboard.php';
		$this->load->view('admin/index',$data);
	}
	else
	{
		redirect('/admin/home/index','refresh');
	}
	}
	
     function login()
     {
     if($this->users_db->isLoggedIn())
     {
          redirect('/admin/home/dashboard','refresh');
     } 
     else 
     {
          $this->load->library('form_validation');
          $this->form_validation->set_rules('username','Login','required');
          $this->form_validation->set_rules('password','Mot de passe','required');
          if(!$this->form_validation->run()){
                    			$data['main']= 'login';

		$this->load->view('index',$data);
          } else {
               $username = $this->input->post('username');
               $password = $this->input->post('password');
               $validCredentials = $this->users_db->validCredentials($username,$password); 

               if($validCredentials){
                    redirect('/admin/home/dashboard','refresh');
               } else {
                    $data['error_credentials'] = 'Wrong Username/Password';
                         			$data['main']= 'login';

		$this->load->view('index',$data);
               }
          }
     }
}
	function logOff()
	{
		$this->users_db->logOff();
		
		redirect('/admin/home','refresh');
			
		
		
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */